<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
  xmlns:ui="http://java.sun.com/jsf/facelets"
  xmlns:h="http://java.sun.com/jsf/html"
  xmlns:f="http://java.sun.com/jsf/core"
  xmlns:a4j="http://richfaces.org/a4j"
  xmlns:zanata="http://java.sun.com/jsf/composite/zanata">

<ui:composition template="../WEB-INF/template/template.xhtml">

  <f:metadata>
    <f:viewParam name="username" value="#{userAction.username}" />
    <f:event type="preRenderView" listener="#{userSearch.loadUsers}"/>
    <f:event type="preRenderView" listener="#{identity.checkRole('admin')}"/>
  </f:metadata>


  <ui:define name="page_title">#{msgs['jsf.ManageUsers']}</ui:define>
  <ui:define name="center_content">
    <zanata:confirm-modal id="deleteUserModal"
      title="#{msgs['jsf.user.delete.title']}"
      message="#{msgs['jsf.AreYouSureYouWishToDeleteThisUserThisActionCannotBeUndone']}"
      onresponse="onDeleteUserResponse" />
    <zanata:confirm-modal id="eraseUserModal"
      title="#{msgs['jsf.user.erase.title']}"
      message="#{msgs['jsf.AreYouSureYouWishToEraseThisUserThisActionCannotBeUndone']}"
      onresponse="onEraseUserResponse" />

    <h:form>
      <a4j:jsFunction name="deleteUser"
        action="#{userAction.deleteUser(username)}" render="usermanagerForm,
      userSearch-pager, userSearch-page-info, userSearchBottom-pager, userSearchBottom-page-info">
        <a4j:param name="val1" assignTo="#{username}"/>
      </a4j:jsFunction>
      <a4j:jsFunction name="eraseUser"
        action="#{userAction.eraseUser(username)}" render="usermanagerForm,
      userSearch-pager, userSearch-page-info, userSearchBottom-pager, userSearchBottom-page-info">
        <a4j:param name="val1" assignTo="#{username}"/>
      </a4j:jsFunction>
    </h:form>

    <script type="text/javascript">
      var selectedUser;
      function setSelectedUser(user) {
        selectedUser = user;
      }
      function onDeleteUserResponse(del) {
        if (del) {
          deleteUser(selectedUser);
        } else {
          selectedUser = null;
        }
        zanata.modal.hide('#deleteUserModal');
      }
      function onEraseUserResponse(del) {
        if (del) {
          eraseUser(selectedUser);
        } else {
          selectedUser = null;
        }
        zanata.modal.hide('#eraseUserModal');
      }
    </script>

    <div class="g--centered">
      <div class="g__item w--2-3 l--push-bottom-half">
        <p class="txt--meta l--push-all-0">
          <a href="/admin/home">Administration</a>
        </p>

        <h1 class="l--push-all-0">#{msgs['jsf.ManageUsers']}</h1>


        <div class="panel l--push-top-1">
          <div class="panel__header">
            <div class="panel__header__actions">
              <div id="rolemanage-more-actions"
                class="dropdown dropdown--header dropdown--small dropdown--right dropdown--inline js-dropdown">
                <a class="dropdown__toggle js-dropdown__toggle" href="#"
                  title="#{msgs['jsf.tooltip.MoreActions']}">
                  <i class="i i--ellipsis"></i>
                </a>
                <ul class="dropdown__content js-dropdown__content"
                  role="content"
                  aria-labelledby="dropdownContent">
                  <li>
                    <h:link styleClass="i__item--right"
                      outcome="/admin/create_user.xhtml"
                      rendered="#{identity.hasPermission('seam.account', 'create')}"
                      value="#{msgs['jsf.CreateNewUser']}">
                      <i class="i i--add i__item__icon"></i>
                    </h:link>
                  </li>
                </ul>
              </div>
            </div>
            <h2 class="panel__heading">#{msgs['jsf.admin.manageuser.header']}</h2>
          </div>
          <div class="panel__sub-header js-reveal">
            <zanata:list-filter status="userLoader"
              listId="usermanagerForm"
              render="usermanagerForm, userSearchBottom-pager, userSearchBottom-page-info"
              id="userSearch" iconClass="i--users"
              placeholder="#{msgs['jsf.SearchUsers']}"
              actionBean="#{userAction.userFilter}"/>
          </div>

          <a4j:status name="userLoader">
            <f:facet name="start">
              <zanata:loader/>
            </f:facet>
          </a4j:status>

          <h:form id="usermanagerForm" styleClass="l--push-bottom-1 ">
            <ul class="list--stats">
              <ui:repeat value="#{userAction.userFilter.currentPage}"
                var="user">
                <li class="list__item--actionable">
                  <div class="list__item__action">
                    <div
                      class="dropdown dropdown--small dropdown--inline dropdown--single list__item__dropdown js-dropdown">
                      <a href="#" title="Options"
                        class="dropdown__toggle js-dropdown__toggle txt--meta">
                        <span class="is-invisible">Options</span>
                      </a>
                      <ul class="dropdown__content js-dropdown__content">
                        <ui:fragment
                          rendered="#{identity.hasPermission('seam.user', 'delete')}">
                          <li>
                            <a href="#" class="i__item--right txt--danger" onclick="setSelectedUser('#{user}')"
                              data-toggle="modal" data-target="#deleteUserModal">
                              #{msgs['jsf.Delete']}
                              <i class="i i--cancel i__item__icon"></i>
                            </a>
                          </li>
                        </ui:fragment>
                        <ui:fragment
                          rendered="#{identity.hasPermission('seam.user', 'delete')}">
                          <li>
                            <a href="#" class="i__item--right txt--danger" onclick="setSelectedUser('#{user}')"
                              data-toggle="modal" data-target="#eraseUserModal">
                              #{msgs['jsf.user.erase.title']}
                              <i class="i i--cancel i__item__icon"></i>
                            </a>
                          </li>
                        </ui:fragment>
                      </ul>
                    </div>
                  </div>

                  <h:link outcome="/admin/userdetail.xhtml"
                    disabled="#{not identity.hasPermission('seam.user', 'update')}">
                    <div class="list__item__content">
                      <div class="list__item__info">
                        <h3 class="list__title" title="#{identityManager.isUserEnabled(user) ? '' : msgs['jsf.account.disable.tooltip']}">
                          <span
                            class="#{identityManager.isUserEnabled(user) ? '' : 'txt--danger'}">
                            <ui:fragment
                              rendered="#{not identityManager.isUserEnabled(user)}">
                              <i class="i i--lock"></i>
                            </ui:fragment>
                            #{user}
                          </span>
                          <h:panelGroup layout="span" styleClass="txt--meta l--push-h-quarter"
                            rendered="#{not userSearch.getUserRoles(user).isEmpty()}">
                            [#{userSearch.getUserRoles(user)}]
                          </h:panelGroup>
                        </h3>
                        <span class="list__item__meta">
                          #{userAction.getEmail(user)}
                        </span>
                      </div>
                    </div>
                    <f:param name="username" value="#{user}"/>
                  </h:link>
                </li>
              </ui:repeat>
            </ul>
          </h:form>
          <zanata:list-filter status="userLoader"
            bottomPanel="true"
            listId="usermanagerForm"
            render="usermanagerForm, userSearch-pager, userSearch-page-info"
            id="userSearchBottom" iconClass="i--users"
            placeholder="#{msgs['jsf.SearchUsers']}"
            actionBean="#{userAction.userFilter}"/>
        </div>
      </div>
    </div>
  </ui:define>

</ui:composition>
</html>
